if(!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.envDef.envList.EnvList")) {
    jsx3.require("jsx3.gui.LayoutGrid")
    /**
     * Env List
     */
    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.envDef.envList", function (envListp) {
    });

    /**
     * EnvList
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.envDef.envList.EnvList",
            jsx3.gui.LayoutGrid,
            null,
            function (envList) {

                envList.prototype.selection = new jsx3.util.List(0);
                envList.prototype.firstTime;
                /**
                 * instance initializer
                 * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
                 * @param vntLeft {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
                 * @param vntTop {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
                 * @param vntWidth {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
                 * @param vntHeight {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
                 */
                envList.prototype.init = function(strName, vntLeft, vntTop, vntWidth, vntHeight) {
                    //call constructor for super class (the super expects the name of the object and the function that it is an instance of)
                    this.jsxsuper(strName, vntLeft, vntTop, vntWidth, vntHeight);
                };

                envList.prototype.subscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_EDIT_SUCCESSEFUL, me, me.refreshEnvList);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ADD_SUCCESSEFUL, me, me.refreshEnvList);
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ADD_CANCELED, me, me.canceledModify);

                    this.mtxList = this.getDescendantOfName("mtxEnvironments27A");
                    this.firstTime = true;
                    this.callgetEnvironments();
                    this.loadPermissionButtons();
                }

                envList.prototype.unsubscribeAll = function() {
                    var me = this;
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_EDIT_SUCCESSEFUL, me);
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ADD_SUCCESSEFUL, me);
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ADD_CANCELED, me);

                }

                envList.prototype.callgetEnvironments = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/envDef/envList/rules/getEnvironmentsRules.xml",
                            "getEnvironments",
                            "Get environments");
                    //subscribe and call
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetEnvironmentsSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.ongetEnvironmentsError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.ongetEnvironmentsInvalid);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getEnvironmentEndpointURL());
                    objService.doCall();
                };

                envList.prototype.ongetEnvironmentsSuccess = function(objEvent) {
                    var array = new Array();
                    var iter = this.selection.iterator();
                    while(iter.hasNext()) {
                        var id = iter.next();
                        this.mtxList.selectRecord(id);
                        array.push(id);
                    }
                    this.publishSelect(array);
                    this.mtxList.repaintData();
                };

                envList.prototype.ongetEnvironmentsError = function(objEvent) {
                };

                envList.prototype.ongetEnvironmentsInvalid = function(objEvent) {
                };

                envList.prototype.tryToRemoveEnvironments = function() {
                    if(this.selection.size == 0) {
                        tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@@noEnv27A"), tibco.admin.Main.getServer().getDynamicProperty("@@selectEnv27A"));
                    }
                    var me = this ;
                    tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@deleteEnv27A"), tibco.admin.Main.getServer().getDynamicProperty("@@deleteConfirmEnv27A"), function(dialog) {
                        dialog.doClose();
                        me.callremoveEnvironments()
                    }, null)
                }

                envList.prototype.callremoveEnvironments = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/envDef/envList/rules/removeEnvironmentsRules.xml",
                            "removeEnvironments",
                            "remove selected environments");
                    //subscribe and call
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onremoveEnvironmentsSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onremoveEnvironmentsError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onremoveEnvironmentsInvalid);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getEnvironmentEndpointURL());
                    objService.doCall();
                };

                envList.prototype.onremoveEnvironmentsSuccess = function(objEvent) {
                    // No item in list should be in selected mode
                    this.entityId = null;
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_REMOVE_SUCCESSEFUL;
                    tibco.admin.PubSub.publish(msg);
                    this.callgetEnvironments();

                };

                envList.prototype.onremoveEnvironmentsError = function(objEvent) {
                };

                envList.prototype.onremoveEnvironmentsInvalid = function(objEvent) {
                };

                envList.prototype.publishSelect = function(strRECORDIDS) {
                    var strRowID = null;
                    var record = null;
                    if(this.ignoreSelects) {
                        var cacheDoc = this.mtxList.getXML();
                        if(cacheDoc) {
                            var recordIter = cacheDoc.getXML().selectNodes("//record[@jsxselected='1']");
                            while(recordIter.hasNext()) {
                                record = recordIter.next();
                                record.setAttribute("jsxselected", 0);
                            }
                            var iter = this.selection.iterator();
                            while(iter.hasNext()) {
                                var jsxid = iter.next();
                                record = cacheDoc.getXML().selectSingleNode("//record[@jsxid='" + jsxid + "']");
                                if(record) {
                                    record.setAttribute("jsxselected", 1);
                                }
                            }
                        }
                        this.mtxList.repaint();
                        return;
                    }
                    else {
                        this.selection.clear();
                        var nodes = this.mtxList.getSelectedNodes();
                        var selectionSize = nodes.size();
                        for(var i = 0; i < selectionSize; i++) {
                            this.selection.add(nodes.getItem(i).getAttribute('jsxid'));
                        }
                        if(selectionSize == 1) {
                            record = nodes.getItem(0);
                            strRowID = record.getAttribute("jsxid");
                        }
                    }
                    this.entityId = strRowID;

                    if(record) {
                        var msg = new Object();
                        msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED
                        msg.cacheID = this.mtxList.getXMLId();
                        msg.environmentID = this.entityId;
                        msg.environmentName = record.getAttribute("name");
                        msg.hasEdit = record.getAttribute("hasEdit");
                        msg.hasOwner = record.getAttribute("hasOwner");
                        tibco.admin.PubSub.publish(msg);
                    }
                    else { //this is for when no record is selected
                        var msg = new Object();
                        msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED
                        msg.environmentID = null;
                        tibco.admin.PubSub.publish(msg);
                    }
                    this.updateButtons();
                }

                /**
                 * This function triggers an add new environment action. It disables all buttons,
                 * desslects the list of environments and sends a message to general info component.
                 * General info component has the responsibility to provide the user with a UI to adding a new environment
                 * or cancel the action. In both cases the generl info component should send a message if the action is
                 * cancelled, was successful or failed, so taht this component (environment list) can decide on further action
                 * for example enableing, disabeling the buttons, refreshing the list and etc...
                 */
                envList.prototype.publishAdd = function() {
                    // deselect records in matrix
                    this.mtxList.deselectAllRecords()


                    // publish message to everybody who is interested in it
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_REQUIRE_NEW;
                    tibco.admin.PubSub.publish(msg);
                }

                /**
                 * This function triggers a refresh action for the master.
                 */
                envList.prototype.publishRefresh = function() {
                    // preserve selection information
                    var nodes = this.mtxList.getSelectedNodes();
                    var size = nodes.size();
                    this.selection.clear();
                    for(var i = 0; i < size; i++) {
                        this.selection.add(nodes.getItem(i).getAttribute('entityId'));
                    }

                    // get fresh data
                    this.callgetEnvironments();
                }

                envList.prototype.refreshEnvList = function(objEvent) {
                    this.entityId = objEvent.entityId;
                    this.selection.clear();
                    this.selection.add(this.entityId);
                    this.callgetEnvironments();
                }

                envList.prototype.updateButtons = function() {
                    var nodes = this.mtxList.getSelectedNodes();
                    var count = nodes.size();
                    var notOwner = 0;
                    var iter = nodes.iterator();
                    while(iter.hasNext()) {
                        var node = iter.next();
                        if(node.getAttribute("hasOwner") != "true") {
                            notOwner++;
                        }
                    }
                    this.getDescendantOfName("btnAdd27A").setEnabled((!this.ignoreSelects) ? jsx3.gui.Form.STATEENABLED : jsx3.gui.Form.STATEDISABLED, true);
                    this.getDescendantOfName("btnDelete27A").setEnabled(((count > 0) && !this.ignoreSelects && (notOwner == 0)) ? jsx3.gui.Form.STATEENABLED : jsx3.gui.Form.STATEDISABLED, true);
                }

                envList.prototype.canceledModify = function(objEvent) {
                    if(this.entityId) {
                        this.mtxList.selectRecord(this.entityId);
                        this.mtxList.repaintData();
                    }
                    this.updateButtons();
                }


                /****************************************************************
                 *  Set permission and remove permission
                 *  Author: Alex Han
                 *  Date: 06-28-2007
                 ****************************************************************/

                envList.prototype.loadPermissionButtons = function() {
                    var domNode = this.getDescendantOfName("panePermissionsButtons27A")
                    this.permissions = tibco.admin.Main.loadUIResource(domNode, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/setPermissions.xml", true)
                    this.permissions.initialize("Environment", "jsxid", "name", this.mtxList)
                    var me = this
                    this.mtxList.switchPermissionBtn = function() {
                        me.permissions.switchPermissionBtn()
                    }
                }

                envList.prototype.beginEditMode = function() {
                    this.ignoreSelects = true;
                    this.updateButtons();
                }

                envList.prototype.endEditMode = function() {
                    this.ignoreSelects = false;
                    this.updateButtons();
                }

            });
}
